/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateMessage.java
 *
 * Created on Sep 25, 2011, 6:19:03 AM
 */
package azhar.view.control;

import azhar.com.Messages;
import azhar.com.Separate;
import azhar.engine.CountCharacter;
import azhar.engine.MessageService;
import azhar.view.Main;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Al-Insan
 */
public class CreateMessage extends javax.swing.JDialog {

    private static final int TOTAL_TEXT = 1920;
    private int x = 1;
    private Main main;
    private MessageService service;
    private Finder find;
    private String no = "";
    CountCharacter countChar = new CountCharacter();

    /** Creates new form CreateMessage */
    public CreateMessage(Main parent, boolean modal) {
        super(parent, modal);
        main = parent;
        parent.setEnabled(false);
        initComponents();
    }

    public CreateMessage(Main parent, boolean modal, Object number) {
        super(parent, modal);
        main = parent;
        parent.setEnabled(false);
        initComponents();
        content.setEditable(modal);
        tfNumber.setText(String.valueOf(number));
    }

    public CreateMessage(Main parent, boolean modal, Object number, Object text) {
        super(parent, modal);
        main = parent;
        parent.setEnabled(false);
        initComponents();
        content.setEditable(modal);
        tfNumber.setText(String.valueOf(number));
        content.setText(String.valueOf(text));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popupMenu = new javax.swing.JPopupMenu();
        jLabel1 = new javax.swing.JLabel();
        tfNumber = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        content = new javax.swing.JTextArea();
        text = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Kepada");

        jButton1.setText("Tambah");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        content.setColumns(20);
        content.setRows(5);
        content.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                contentKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                contentKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(content);

        text.setText("1000 / 1");

        jButton2.setText("Kirim");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Simpan");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(text)))
                .addGap(33, 33, 33))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(18, 18, 18)
                .addComponent(text)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-421)/2, (screenSize.height-350)/2, 421, 350);
    }// </editor-fold>//GEN-END:initComponents

    private void contentKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_contentKeyTyped
        setCountText(countChar.count(content), evt);
}//GEN-LAST:event_contentKeyTyped

    private void contentKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_contentKeyReleased
        setCountText(countChar.count(content), evt);
    }//GEN-LAST:event_contentKeyReleased

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            ArrayList<azhar.com.Messages> messages = new ArrayList<Messages>();
            messages.add(new Messages(tfNumber.getText(), content.getText()));
            main.sendMessage(messages);
        } catch (Exception ex) {
            Logger.getLogger(CreateMessage.class.getName()).log(Level.SEVERE, null, ex);
        }
        close();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        close();
    }//GEN-LAST:event_formWindowClosing

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        main.save(tfNumber.getText(), content.getText());
        close();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        initializePopup();
        showPanel();
    }//GEN-LAST:event_jButton1ActionPerformed

    public void close() {
        dispose();
        main.setEnabled(true);
        main.setVisible(true);
    }

    public void setNumber(String[] n) {
        no = tfNumber.getText() + new Separate().separated(n);
        tfNumber.setText(no);
    }

    public void setCountText(int ch, KeyEvent evt) {
        int t = TOTAL_TEXT - ch;
        if (ch > (160 * x)) {
            x++;
        }
        if (x > 12) {
            x = 12;
        }
        if (t > 0) {
            text.setText(t + " / " + x);
        } else {
            t = 0;
            text.setText(t + " / " + x);
            if (evt.getKeyCode() == KeyEvent.VK_DELETE || evt.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
                content.setEditable(true);
            } else {
                content.setEditable(false);
                content.setText(countChar.getText());
            }
        }
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea content;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu popupMenu;
    private javax.swing.JLabel text;
    private javax.swing.JTextField tfNumber;
    // End of variables declaration//GEN-END:variables

    protected void initializePopup() {
        if (find == null) {
            find = new Finder(main, popupMenu, this);
            popupMenu.add(BorderLayout.NORTH, find);
        }
    }

    private void showPanel() {
        if (!popupMenu.isShowing()) {
            popupMenu.show(tfNumber, 0, tfNumber.getHeight());
            popupMenu.requestFocus();
        }
    }
}
